<?php

namespace DebugComponent\Plugin;

class Message implements \DebugComponent\IMessagePlugin, \DebugComponent\IRenderable {

    public function  __construct() {
        $this->messages = array();
    }

    public function setRenderer(\DebugComponent\IRenderer $renderer) {
        $this->renderer = $renderer;
    }

    public function getListenMessages() {
        return array('\DebugComponent\IMessage');
    }

    public function write(\DebugComponent\IMessage $message) {
        $this->messages[] = $message;
    }

    public function getTitle() {
        return 'Messages';
    }

    public function getSubtitle() {
        return count($this->messages) . ' / ' . $this->getErrorMessagesCount();
    }

    public function render() {
        $data = new \DebugComponent\ParameterHolder();
        $data->set('messages', $this->messages);
        $data->set('title', $this->getTitle());
        $data->set('subtitle', $this->getSubtitle());
        return $this->renderer->render($data);
    }

    private function getErrorMessagesCount() {
        $result = 0;
        foreach ($this->messages as $message) {
            if ($message instanceof \DebugComponent\Message\Error) {
                $result++;
            }
        }
        return $result;
    }

    private $messages;
    private $renderer;
}